import { _decorator, Component, Node, Sprite } from 'cc';
import { SunManager } from './manager/SunManager';
const { ccclass, property } = _decorator;

@ccclass('Card')
export class Card extends Component {
    // 卡牌状态
    private cardState: CardState = CardState.Cooling;
    @property(Node)
    public cardLight: Node = null;// 卡牌亮的节点
    @property(Node)
    public cardGray: Node = null;// 卡牌暗的节点
    @property(Sprite)
    public cardMask: Sprite = null;// 卡牌正面
    @property(Number)
    public cdTime: number = 2;
    public cdTimer: number = 0;
    @property({ type: Number, tooltip: "卡牌需要的阳光数" })
    private needSunPoint: number = 50;
    start() {
        this.cdTimer = this.cdTime;
    }

    update(deltaTime: number) {
        switch (this.cardState) {
            case CardState.Cooling:
                this.coolingUpdate(deltaTime)
                break;
            case CardState.WaitingSun:
                this.waitingSunUpdate()
                break;
            case CardState.Ready:
                this.readyUpdate()
                break;
        }

    }

    private readyUpdate() {
    }
    private waitingSunUpdate() {
        if (this.needSunPoint <= SunManager.getInstance().SunPoint) {
            this.transitionToReady()
        }
    }
    transitionToReady() {
        this.cardState = CardState.Ready;
        this.cardLight.active = true;
        this.cardGray.active = false;
        this.cardMask.node.active = false;
    }

    private coolingUpdate(dt: number) {
        this.cdTimer -= dt;
        this.cardMask.fillRange = - (this.cdTimer / this.cdTime);
        if (this.cdTimer <= 0) {
            this.transitionToWaitingSun()
        }
    }

    private transitionToWaitingSun() {
        this.cardState = CardState.WaitingSun;
        this.cardLight.active = false;
        this.cardGray.active = true;
        this.cardMask.node.active = false;
    }


}

export enum CardState {
    Cooling,// 冷却中
    WaitingSun, // 冷却好了没太阳
    Ready, // 冷却好了有太阳

}

